package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.constant.MessageConst;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Reference
    SetmealService setmealService;

    @Autowired
    JedisPool jedisPool;

    @RequestMapping("/getSetmeal")
    public Result getSetmeal() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        //从redis数据库获取套餐信息
        String setmeals = jedisPool.getResource().get("套餐列表数据添加到redis数据库");
        //判断
        if (setmeals==null){
            //从数据库查询套餐信息
            setRedis();
        }

        setmeals=jedisPool.getResource().get("套餐列表数据添加到redis数据库");

        List<Setmeal> list = setmealService.findAll();

        String str = mapper.writeValueAsString(list);

        if (setmeals.equals(str)){
            return new Result(true, MessageConst.QUERY_SETMEALLIST_SUCCESS, list);
        }
        //将字符串转换成list对象
        List<Setmeal> setmealList = mapper.readValue(setmeals, List.class);
        //返回查询结果
        return new Result(true, MessageConst.QUERY_SETMEALLIST_SUCCESS, setmealList);

    }

    private void setRedis() throws JsonProcessingException {

        ObjectMapper mapper = new ObjectMapper();

        List<Setmeal> setmealList = setmealService.findAll();
        //将结果转换为json字符串
        String str = mapper.writeValueAsString(setmealList);
        //添加rids数据库的套餐列表
        jedisPool.getResource().set("套餐列表数据添加到redis数据库",str);
    }

    @RequestMapping("/findDetailById")
    public Result findDetailById(Integer id) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        //从redis数据库获取套餐详情信息
        String setmeal = jedisPool.getResource().get("套餐详情添加到redis数据库");

        if (setmeal==null){
            Setmeal setmeal1  = setmealService.findDetailById(id);
            //将结果转换为json字符串
            String str = mapper.writeValueAsString(setmeal1);
            //添加rids数据库的套餐列表
            jedisPool.getResource().set("套餐详情添加到redis数据库",str);

        }

        setmeal=jedisPool.getResource().get("套餐详情添加到redis数据库");

        Setmeal setmeal1 =  setmealService.findDetailById(id);

        String str = mapper.writeValueAsString(setmeal1);

        if (setmeal.equals(str)){
            return new Result(true, MessageConst.QUERY_SETMEALLIST_SUCCESS, setmeal1);
        }

        Setmeal setmeal2 = mapper.readValue(setmeal, Setmeal.class);

        return new Result(true,MessageConst.QUERY_SETMEAL_SUCCESS,setmeal2);
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        log.debug("SetmealController:findById: " + id);
        Setmeal setmeal = setmealService.findById(id);
        log.debug("根据id查询套餐成功");
        return new Result(true,MessageConst.QUERY_SETMEAL_SUCCESS,setmeal);
    }



}
